package com.mdlib.helper.ui.fragments.sect.my;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.ljlib.core.base.BaseFragment;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.common.ContentType;
import com.mdlib.helper.model.entity.OrderEntity;
import com.mdlib.helper.ui.UIHelper;
import com.mdlib.helper.ui.adapter.SendMyOrderAdapter;
import com.mdlib.helper.ui.widget.springview.DefaultFooter;
import com.mdlib.helper.ui.widget.springview.HelperHeads;
import com.mdlib.helper.ui.widget.springview.SpringView;
import com.mdlib.helper.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 作者：HQY on 16/11/22 17:19
 * 邮箱：hqy_xz@126.com
 */

public class SendOngoingOrderFragment extends BaseFragment {
    @Bind(R.id.rv_ongoing_order)
    RecyclerView mRvOngoingOrder;
    @Bind(R.id.sv_send_ongoing)
    SpringView mSvSendOngoing;
    private List<OrderEntity> orderList = new ArrayList<>();

    private int pageNumber = 1;

    private SendMyOrderAdapter mOrderAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_send_order_ongoing;
    }

    @Override
    protected void initView(View view) {
        init();
    }

    @Override
    public void onStart() {
        super.onStart();
        orderList.clear();
        pageNumber = 1;
        getSendOngoingOrder();
    }

    public void init() {

        mOrderAdapter = new SendMyOrderAdapter(orderList);
        mRvOngoingOrder.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvOngoingOrder.setAdapter(mOrderAdapter);
        mRvOngoingOrder.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {

            }

            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                UIHelper.showHomeRelationThree(getActivity(), ContentType.ORDERDETAILS, orderList.get(position).getId()+"");
            }
        });
        mSvSendOngoing.setType(SpringView.Type.FOLLOW);
        mSvSendOngoing.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                orderList.clear();
                pageNumber = 1;
                getSendOngoingOrder();
            }

            @Override
            public void onLoadmore() {
                getSendOngoingOrder();
            }
        });
        mSvSendOngoing.setHeader(new HelperHeads(getActivity()));
        mSvSendOngoing.setFooter(new DefaultFooter(getActivity()));
//        getSendOngoingOrder();
    }


    private void getSendOngoingOrder() {
        startLoadingDialog(true);
        addSubscribe(DataManager.getInstance().getUserApi()
                .getOngoingOrder(pageNumber + "")
                .compose(RxHelper.<ArrayList<OrderEntity>>defaultHandleResult())
                .subscribe(new RxSubscribe<ArrayList<OrderEntity>>() {
                    @Override
                    protected void onSucc(ArrayList<OrderEntity> orderEntities) {
                        stopLoadingDialog();
                        pageNumber++;
                        updateList(orderEntities);
                        mSvSendOngoing.onFinishFreshAndLoad();
                    }

                    @Override
                    protected void onFail(String msg) {
                        stopLoadingDialog();
                        ToastUtil.showToast(msg);
                        mSvSendOngoing.onFinishFreshAndLoad();
                    }
                }));
    }

    private void updateList(List<OrderEntity> orders) {
        if (orders.size() > 0 && pageNumber >= 2) {
            for (OrderEntity order : orders) {
                orderList.add(order);
            }
            mOrderAdapter.notifyDataSetChanged();
        } else if (orders.size() <= 0 && pageNumber > 2) {
            pageNumber--;
            ToastUtil.showToast("进行中没有更多数据了!");
        } else if (orders.size() == 0 && pageNumber == 2) {
            orderList.clear();
            mOrderAdapter.notifyDataSetChanged();
        }
    }
}
